package com.chdz.code.utils;

import com.chdz.code.model.ColumnClass;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.*;

/**
 * @Auther: Appet
 * @Date: 2018/8/6 16:03
 * @Description:
 */
public class DBUtils {
    //数据库配置，生成model时使用
    private static final String URL = "jdbc:mysql://192.168.10.210:3306/chdz_my_mps?useUnicode=true&characterEncoding=UTF8&useSSL=false";
    private static final String USER = "chdz";
    private static final String PASSWORD = "chdz2018";
    private static final String DRIVER = "com.mysql.jdbc.Driver";

    public static Connection getConnection() throws Exception{
        Class.forName(DRIVER);
        Connection connection= DriverManager.getConnection(URL, USER, PASSWORD);
        return connection;
    }

    //
    public static Map<String,List<ColumnClass>> getFields(String tableName){
        HashMap<String, List<ColumnClass>> map = new HashMap<>();
        try{
            Connection connection = getConnection();
            DatabaseMetaData databaseMetaData = connection.getMetaData();
            ResultSet resultSet = databaseMetaData.getColumns(null,"%",
                    tableName,"%");
            List<ColumnClass> columnClassList = new ArrayList<>();
            List<ColumnClass> addUpdateRequest = new ArrayList<>();
            List<ColumnClass> objResponse = new ArrayList<>();
            ColumnClass columnClass = null;
            String[] fields = {"id","create_by","create_time","update_by","update_time","del_flag", "remarks"};
            List<String> removeFields = Arrays.asList(fields);
            while(resultSet.next()){
                columnClass = new ColumnClass();
                //获取字段名称
                columnClass.setColumnName(resultSet.getString("COLUMN_NAME"));
                //获取字段类型
                columnClass.setColumnType(resultSet.getString("TYPE_NAME"));
                //转换字段名称，如 sys_name 变成 SysName
                columnClass.setChangeColumnName(CodeGenerateUtils.replaceUnderLineAndUpperCase(resultSet.getString("COLUMN_NAME")));
                //字段在数据库的注释
                columnClass.setColumnComment(resultSet.getString("REMARKS"));
                //字段长度
                columnClass.setColumnSize(resultSet.getInt("COLUMN_SIZE"));
                
                if("id".equals(resultSet.getString("COLUMN_NAME"))){
                	objResponse.add(columnClass);
                }
                
                if(!removeFields.contains(resultSet.getString("COLUMN_NAME"))){
                    addUpdateRequest.add(columnClass);
                    objResponse.add(columnClass);
                }
                columnClassList.add(columnClass);
            }

            map.put("all",columnClassList);
            map.put("reduce",addUpdateRequest);
            map.put("response",objResponse);
        }catch(Exception e){
            e.printStackTrace();
        }
        return map;
    }
}
